package info.thereisonlywe.onenesspathfinder.controllers;

import info.thereisonlywe.onenesspathfinder.ui.UIDelegate;
import info.thereisonlywe.quran.Quran;
import info.thereisonlywe.quran.QuranicVerse;
import info.thereisonlywe.salat.recitation.Dhikr;
import info.thereisonlywe.salat.recitation.imam.DhikrImam;

import java.awt.Toolkit;

public class RecitationController {

	private static DhikrImam imam = new DhikrImam(null, null);

	public static DhikrImam getImam()
	{
		return imam;
	}

	protected static void setImam(DhikrImam i)
	{
		imam = i;
	}

	public static void reciteSection()
	{
		if (!UIDelegate.isDialogOpen()
				&& (!imam.isAlive() || !UIDelegate.isBrowserLocked()))
		{
			QuranicVerse v = UIDelegate.getDisplayedVerse();
			imam.terminate();
			imam.terminate();
			imam = new DhikrImam(new Dhikr(v, Quran.getInSectionVerseCount(v)
					- (v.getVerseNumber() - 1)), UIDelegate.getQuranReciter(),
					UIDelegate.getTextReciter(), false);
			imam.showDialogs(true);
			imam.setReciterSwitchAllowed(true);
			imam.setLoop(UIDelegate.getLoop());
			imam.setVolume(UIDelegate.getVolume());
			imam.begin();
		}
		else
		{
			Toolkit.getDefaultToolkit().beep();
		}
	}

	public static void reciteVerse()
	{
		if (!UIDelegate.isDialogOpen()
				&& (!imam.isAlive() || !UIDelegate.isBrowserLocked()))
		{
			QuranicVerse v = UIDelegate.getDisplayedVerse();
			imam.terminate();
			imam.terminate();
			imam = new DhikrImam(new Dhikr(v), UIDelegate.getQuranReciter(),
					UIDelegate.getTextReciter(), false);
			imam.showDialogs(true);
			imam.setReciterSwitchAllowed(true);
			imam.setLoop(UIDelegate.getLoop());
			imam.setVolume(UIDelegate.getVolume());
			imam.begin();
		}
		else
		{
			Toolkit.getDefaultToolkit().beep();
		}
	}

	public static void reciteCustom(QuranicVerse[] verses, boolean showDialog)
	{
		if ((!imam.isAlive() || !UIDelegate.isBrowserLocked()))
		{// does not check for any open dialogs
			imam.terminate();
			imam.terminate();
			imam = new DhikrImam(new Dhikr(verses),
					UIDelegate.getQuranReciter(), UIDelegate.getTextReciter(),
					false);
			imam.showDialogs(showDialog);
			imam.setReciterSwitchAllowed(true);
			imam.setLoop(UIDelegate.getLoop());
			imam.setVolume(UIDelegate.getVolume());
			imam.begin();
		}
	}

}
